package com.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class CalcLog {
	private String date;
	public CalcLog(String date){
		this.date = date;
	}
	public void calc(){
		String path = "C:/was_logs/"+date+"/";
		File f1 = new File(path+"console.log");
		File f2 = new File(path+"result.txt");
		try {
			BufferedReader br = new BufferedReader(new FileReader(f1));
			BufferedWriter bw = new BufferedWriter(new FileWriter(f2,true));
			String s;
			int i=0;
			while((s=br.readLine())!=null){
				System.out.println(i++);
				if(s.length()>0 && !s.equals("")){
					if(s.indexOf("bha@us.ibm.com")>0 ||s.indexOf("zwpzhang@cn.ibm.com")>0 ||s.indexOf("knakhla@au1.ibm.com")>0 ||s.indexOf("pholian@au1.ibm.com")>0){
						continue;
					}
					if(s.indexOf("==> Parameters:")>0){
						bw.append(s);
						bw.append("\n");
					}
				}
			}
			bw.close();
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void calc2(){
		File f1 = new File("C:/was_logs/"+date+"/result.txt");
		File f2 = new File("C:/was_logs/"+date+"/"+date+".log");
		try {
			BufferedReader br = new BufferedReader(new FileReader(f1));
			String s;
			String date = "";
			while((s=br.readLine())!=null){
				if(!date.equals(s.split(" ")[0]))
				date = s.split(" ")[0];
				
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void calcAll(){
		String path = "C:/was_logs/"+date+"/";
		File f1 = new File(path+"result.txt");
		File f2 = new File(path+"all.txt");
		try {
			BufferedReader br = new BufferedReader(new FileReader(f1));
			BufferedWriter bw = new BufferedWriter(new FileWriter(f2,true));
			String s;
			int i=0;
			Set<String> ids = new HashSet<String>();
			while((s=br.readLine())!=null){
//				System.out.println(i++);
				if(s.length()>0 && !s.equals("")){
					if(s.indexOf("==> Parameters:")>0){
						String[] params = s.split("Parameters: ");
						if(params.length>1 && params[1]!=null && params[1].length()>0 && !params[1].equals("")){
							if(s.indexOf("bha@us.ibm.com")>0 ||s.indexOf("zwpzhang@cn.ibm.com")>0 ||s.indexOf("knakhla@au1.ibm.com")>0 ||s.indexOf("pholian@au1.ibm.com")>0){
								continue;
							}
							System.out.println(i++);
							String[] id = params[1].split("(String)");
							ids.add(id[0]);
						}
					}
				}
			}
			Iterator<String> it = ids.iterator();
			while(it.hasNext()){
//				System.out.println(i++);
				bw.append(it.next());
				bw.append("\n");
			}
			bw.close();
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		String s = "adfad==> Parameters:afd";
//		System.out.println(s.indexOf("==> Parameters:"));
		CalcLog cl = new CalcLog("2016-05-27");
		
		cl.calc();
//		cl.calcAll();
		
		
//		String a = "adsfAFA*ga*af";
//		System.out.println(a.replaceAll("\\*", "-"));
	}
}
